package com.itheima.spring.dao.impl;

import com.itheima.spring.dao.AccountDao;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.SQLException;

@Repository
public class AccountDaoImpl implements AccountDao {

    //定义一个JdbcTemplate成员变量访问数据库  用springIOC自动注入
    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private QueryRunner queryRunner;

    @Override
    public void inUser(String name, int money) {
        //编写sql
        String sql = "update account set money=money + ? where name =?";
        //使用jdbctemplate访问数据库 执行sql
        jdbcTemplate.update(sql, money, name);
    }

    @Override
    public void outUser(String name, int money) {
        //编写sql
        String sql = "update account set money = money - ? where name =?";
        //使用jdbctemplate访问数据库 执行sql
        jdbcTemplate.update(sql, money, name);
    }



    @Override
    public void outUser(Connection connection, String outName, int money) throws SQLException {
        //编写sql
        String sql = "update account set money=money + ? where name =?";
        //使用jdbctemplate访问数据库 执行sql
        queryRunner.update(connection,sql, money, outName);
    }

    @Override
    public void inUser(Connection connection, String inName, int money) throws SQLException {

        //编写sql
        String sql = "update account set money=money + ? where name =?";
        //使用jdbctemplate访问数据库 执行sql
        queryRunner.update(connection,sql, money, inName);
    }
}
